package com.xxx.springboot.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.springboot.mapper.UserMapper;
import com.xxx.springboot.pojo.entity.User;
import com.xxx.springboot.service.UserService;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 *  服务实现类
 * </p>
 * mybatis-plus读写分离：https://www.cnblogs.com/tiancai/p/9110431.html
 *
 * @author gwq
 * @since 2020-04-26
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

	@Autowired
	private UserMapper userMapper;

	@DS("second") // mybatis-plus多数据源：指定从second从库中读取
	@Override
	public User getById(String userId) {
		log.info("getById|userId:{}", userId);
		return userMapper.getById(userId);
	}
	
	@DS  // mybatis-plus多数据源：随机从库（不使用该注解表示读主库）
	@Override
	public User getByName(String username) {
		log.info("getByName|username:{}", username);
		return userMapper.getByName(username);
	}

	@Override
	public int saveUser(User user) {
		log.info("saveUser|user:{}", user.toString());
		return userMapper.insert(user);
	}

	@Override
	public boolean deleteUserById(String userId) {
		log.info("deleteUserById|userId:{}", userId);
		return removeById(userId);
	}
	
}
